#!/bin/bash
#
# todo     todo app developed using python and Flask
# chkconfig: 345 85 15
# pidfile: /var/run/todo.pid

# Source function library.
. /etc/init.d/functions

# Leave blank to disable virtualenv settings
PYTHONENV=/home/python/flask/bin/python

PORT=3127
APPNAME=TODO
PROG=/home/www/python/simple-todo/todo.py
USER=www
RETVAL=0
PIDFILE=/var/run/nginx/todo.pid

start () {
  echo -n $"Starting todo fcgi backend daemon: "
  su - --shell=/bin/bash -p $USER -c "$PYTHONENV $PROG runfcgi --protocol=fcgi -h localhost -p $PORT --daemonize --pidfile=$PIDFILE"
  echo "OK"
}

stop() {
  echo -n $"Stopping todo fcgi backend daemon: "
  if [ -f $PIDFILE ]; then
    kill `cat $PIDFILE`
    rm -rf $PIDFILE
  fi
  echo "OK"
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    if [ -f $PIDFILE ]; then
      PID=`cat $PIDFILE`
    fi
    if [ ! -z $PID ]; then
      echo "$APPNAME is running, pid is $PID"
    else
      echo "$APPNAME is not running"
    fi
    ;;
  restart)
    stop
    sleep 1
    start
    ;;
  *)
    echo $"Usage: $0 {start|stop|restart}"
    exit 1
esac

exit $RETVAL
